<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-tabs v-model="first">
        <el-tab-pane label="角色管理" name="first">
          <el-row style="height:60px">
            <el-button type="primary" icon="el-icon-plus" size="small" @click="isshow = true">新增角色</el-button>
          </el-row>
          <!-- 表格 -->
          <el-table border :data="list">
            <el-table-column align="center" label="序号" width="120" type="index" />
            <el-table-column align="center" label="角色名称" width="240" prop="name" />
            <el-table-column align="center" label="描述" prop="description" />
            <el-table-column align="center" label="操作">
              <template slot-scope="{ row }">
                <!-- <template slot-scope="scope.row"> -->
                <el-button size="small" type="success" @click="privileges(row.id)">分配权限</el-button>
                <el-button size="small" type="primary" @click="editRoles(row.id)">编辑</el-button>
                <el-button size="small" type="danger" @click="delRoles(row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <el-row type="flex" justify="center" align="middle" style="height: 60px">
            <el-pagination
              layout="prev, pager, next"
              :current-page="page.page"
              :total="page.total"
              :page-size="page.pagesize"
              @current-change="changePage"
            />
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="公司信息" name="second">
          <el-alert
            :closable="false"
            show-icon
            type="info"
            title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
          />
          <el-form ref="form" :model="formData" label-width="120px" style="margin-top: 50px">
            <el-form-item label="公司名称">
              <el-input v-model="formData.name" style="width: 400px" disabled />
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input v-model="formData.companyAddress" style="width: 400px" disabled />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="formData.mailbox" style="width: 400px" disabled />
            </el-form-item>
            <el-form-item label="备注">
              <el-input
                v-model="formData.remarks"
                type="textarea"
                :rows="3"
                style="width: 400px"
                disabled
              />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
      <!-- 编辑弹窗 -->
      <el-dialog :title="rowForm.id ? '编辑' : '新增'" :visible="isshow" @close="btnCancel">
        <el-form ref="form" :model="rowForm" :rules="rules" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="rowForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="rowForm.description" />
          </el-form-item>
        </el-form>
        <el-row type="flex" justify="center">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button type="primary" size="small" @click="butOk">确定</el-button>
        </el-row>
      </el-dialog>
      <!-- 权限弹窗 -->
      <el-dialog title="分配权限" :visible="showDialog" @close="btnPermCancel">
        <el-tree
          ref="permData"
          :data="permData"
          :props="defaultProps"
          :default-expand-all="true"
          show-checkbox
          :check-strictly="true"
          :default-checked-keys="checkedTree"
          node-key="id"
        />
        <el-row slot="footer" type="flex" justify="center">
          <el-button size="small" @click="btnPermCancel">取消</el-button>
          <el-button type="primary" size="small" @click="btnPermOk">确定</el-button>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getRoleList,
  getCompanyInfo,
  delRole,
  getRoleDetail,
  putRoleDetail,
  addRoleDetail,
  assignPerm
} from '@/api/setting'
import { getPermissionList } from '@/api/permisson'
import { tranListToTreeData } from '@/utils/index'
export default {
  // 组件名称
  name: '',
  // 局部注册的组件
  components: {},
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data() {
    return {
      first: 'first',
      list: [], // 所有角色数据
      formData: {}, // 公司信息
      page: {
        page: 1, // 页码
        pagesize: 5, // 每页条数
        total: 0 // 记录总数
      },
      isshow: false, // 弹窗的显示与隐藏
      showDialog: false, // 是否显示权限弹窗
      rowForm: {}, // 角色详情
      rules: {
        // 表单校验
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      permData: [], // 权限树形数据
      checkedTree: [], // 根据id获取权限点
      userId: null, // 记录点击后的id
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  // 组件方法
  methods: {
    // 获取角色列表
    async getRoleList() {
      const result = await getRoleList(this.page)
      console.log(result)
      this.list = result.rows
      this.page.total = result.total
    },
    changePage(newpage) {
      this.page.page = newpage // 将页码修改赋值给当前最新页码
      this.getRoleList()
    },
    // 获取公司信息
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.$store.getters.companyId)
    },
    // 根据id删除角色
    async delRoles(id) {
      try {
        await this.$confirm('确认要删除吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await delRole(id)
        this.getRoleList() // 刷新数据
        this.$message.success('删除成功！')
      } catch (err) {
        console.log(err)
      }
    },
    // 编辑与新增角色
    async editRoles(id) {
      // 为了解决闪烁问题，先获取数据，在显示弹窗    // 新方法
      this.rowForm = await getRoleDetail(id)
      this.isshow = true
    },
    // 提交数据
    async butOk() {
      try {
        // 这个只有校验通过，才会执行await的下方内容
        await this.$refs.form.validate()
        // 根据id判断是编辑还是新增
        if (this.rowForm.id) {
          // 编辑
          await putRoleDetail(this.rowForm)
        } else {
          // 新增
          await addRoleDetail(this.rowForm)
        }
        this.getRoleList() // 刷新数据
        this.isshow = false // 关闭弹窗
        this.rowForm = {
          name: '',
          description: ''
        } // 清空表单
        this.$message.success('操作成功！')
      } catch (err) {
        console.log(err)
      }
    },
    // 点击取消
    btnCancel() {
      this.rowForm = {
        name: '',
        description: ''
      }
      this.$refs.form.resetFields() // 表单校验重置
      this.isshow = false
    },
    // 分配权限
    async privileges(id) {
      this.permData = tranListToTreeData(await getPermissionList(), '0')
      const { permIds } = await getRoleDetail(id)
      this.checkedTree = permIds
      this.userId = id
      this.showDialog = true
    },
    // 点击权限确定
    async btnPermOk() {
      // console.log(this.$refs.permData.getCheckedKeys())
      await assignPerm({ permIds: this.$refs.permData.getCheckedKeys(), id: this.userId })
      this.$message.success('分配权限成功')
      this.showDialog = false
    },
    // 点击权限取消
    btnPermCancel() {
      this.checkedTree = []
      this.showDialog = false
    }
  }
}
</script>

<style scoped lang="less"></style>
